package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.appwidget.lib.AppWidgetAdder;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.ComponentBean;
import flc.ast.databinding.ActivityEditWeatherBinding;
import flc.ast.utils.MyWidgetLong;
import flc.ast.utils.MyWidgetShort;
import flc.ast.widget.calendar.Calendar10Widget;
import flc.ast.widget.calendar.Calendar1Widget;
import flc.ast.widget.calendar.Calendar2Widget;
import flc.ast.widget.calendar.Calendar3Widget;
import flc.ast.widget.calendar.Calendar4Widget;
import flc.ast.widget.calendar.Calendar5Widget;
import flc.ast.widget.calendar.Calendar6Widget;
import flc.ast.widget.calendar.Calendar7Widget;
import flc.ast.widget.calendar.Calendar8Widget;
import flc.ast.widget.calendar.Calendar9Widget;
import flc.ast.widget.time.Time10Widget;
import flc.ast.widget.time.Time1Widget;
import flc.ast.widget.time.Time2Widget;
import flc.ast.widget.time.Time3Widget;
import flc.ast.widget.time.Time4Widget;
import flc.ast.widget.time.Time5Widget;
import flc.ast.widget.time.Time6Widget;
import flc.ast.widget.time.Time7Widget;
import flc.ast.widget.time.Time8Widget;
import flc.ast.widget.time.Time9Widget;
import flc.ast.widget.weather.Weather1Widget;
import flc.ast.widget.weather.Weather2Widget;
import flc.ast.widget.weather.Weather3Widget;
import flc.ast.widget.weather.Weather4Widget;
import flc.ast.widget.weather.Weather5Widget;
import flc.ast.widget.weather.Weather6Widget;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.constant.Extra;
import stark.common.basic.device.BatteryUtil;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.RxUtil;

public class EditWeatherActivity extends BaseAc<ActivityEditWeatherBinding> {

    private int mSelWeather = 0;
    private List<ComponentBean> mComponentBeanList = new ArrayList<>();

    public static void start(Context context, int pos) {
        Intent intent = IntentUtil.getIntent(context, EditWeatherActivity.class);
        intent.putExtra(Extra.POS, pos);
        context.startActivity(intent);
    }

    @Override
    protected int onCreate() {
        return R.layout.activity_edit_weather;
    }

    @Override
    protected void initView() {
        mSelWeather = getIntent().getIntExtra(Extra.POS, 0);

        mDataBinding.tvEditWeatherAdd.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvEditWeatherAdd:
                addWidget();
                break;
            default:
                break;
        }
    }

    @Override
    protected void initData() {
        //显示小组件布局
        setLayout();
    }

    //添加小组件
    private void addWidget() {
        if (FastClickUtil.isFastClick()) {
            return;
        }
        switch (mSelWeather) {
            case 1:
                createWidget(R.layout.widget_weather1, MyWidgetLong.class);
                break;
            case 2:
                createWidget(R.layout.widget_weather2, MyWidgetShort.class);
                break;
            case 3:
                createWidget(R.layout.widget_weather3, MyWidgetShort.class);
                break;
            case 4:
                createWidget(R.layout.widget_weather4, MyWidgetLong.class);
                break;
            case 5:
                createWidget(R.layout.widget_weather5, MyWidgetLong.class);
                break;
            case 6:
                createWidget(R.layout.widget_weather6, MyWidgetLong.class);
                break;
            default:
                break;
        }
    }

    //创建小组件
    private void createWidget(int layout, Class<? extends AppWidgetProvider> cls) {
        //要想应用能够接收到开机广播自启动，需具备以下两个条件，1、申请到悬浮窗权限 2、请求忽略电池优化(荣耀pro20手机实际测试到可以不用申请悬浮窗权限也能收到开机广播)
        //1、申请悬浮窗权限(如果权限请求被用户拒绝，是否每次进来都要请求请开发人员自己做处理，这里只是demo，直接每次都请求了)
        getPermission(layout, cls);
        //2、请求忽略电池优化(如果请求被用户拒绝，是否每次进来都要请求请开发人员自己做处理，这里只是demo，直接每次都请求了)
        BatteryUtil.reqIgnoreBatteryOptimizations(mContext);
    }

    //获取权限
    private void getPermission(int layout, Class<? extends AppWidgetProvider> cls) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PermissionUtils.requestDrawOverlays(new PermissionUtils.SimpleCallback() {
                @Override
                public void onGranted() {
                    showDialog(getString(R.string.create_ing));
                    RxUtil.create(new RxUtil.Callback<Boolean>() {
                        private String filePath;

                        @Override
                        public void doBackground(ObservableEmitter<Boolean> emitter) {
                            Bitmap bitmap = null;
                            switch (mSelWeather) {
                                case 1:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeWeather1.rlWeather1View);
                                    break;
                                case 2:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeWeather2.rlWeather2View);
                                    break;
                                case 3:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeWeather3.rlWeather3View);
                                    break;
                                case 4:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeWeather4.rlWeather4View);
                                    break;
                                case 5:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeWeather5.rlWeather5View);
                                    break;
                                case 6:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeWeather6.rlWeather6View);
                                    break;
                                default:
                                    break;
                            }
//                            ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                            emitter.onNext(true);
                        }

                        @Override
                        public void accept(Boolean obj) {
                            mComponentBeanList.add(new ComponentBean(filePath, 3, mSelWeather, 0, "", 0, "", ""));
                            dismissDialog();
                            AppWidgetAdder.getInstance().addAppWidget(layout, cls);
                        }
                    });
                }

                @Override
                public void onDenied() {
                    ToastUtils.showShort(R.string.permission_no_tips);
                }
            });
        }
    }

    //展示对应布局
    private void setLayout() {
        switch (mSelWeather) {
            case 1:
                mDataBinding.includeWeather1.rlWeather1View.setVisibility(View.VISIBLE);
                break;
            case 2:
                mDataBinding.includeWeather2.rlWeather2View.setVisibility(View.VISIBLE);
                break;
            case 3:
                mDataBinding.includeWeather3.rlWeather3View.setVisibility(View.VISIBLE);
                break;
            case 4:
                mDataBinding.includeWeather4.rlWeather4View.setVisibility(View.VISIBLE);
                break;
            case 5:
                mDataBinding.includeWeather5.rlWeather5View.setVisibility(View.VISIBLE);
                break;
            case 6:
                mDataBinding.includeWeather6.rlWeather6View.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

}